package com.mirror.utils.base;

import java.io.File;

import com.mirror.llk.R;
import com.mirror.llk.utils.Constant;
import com.mirror.utils.io.FileUtils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.KeyEvent;

/**
 * Activity基类
 * <p>
 * Copyright: Copyright (c) 2013-4-26 上午10:50:37
 * <p>
 * Company: 北京幻方朗睿技术有限公司
 * <p>
 * Author: 李晖
 * <p>
 * Version: 1.0
 * <p>
 */
public class BaseActivity extends Activity {

	private static String TAG = "BaseActivity";

	/** Activity销毁时是否有动画效果 */
	protected boolean isAnimation = true;

	/** 剪切图片 */
	protected static String ACTION_CROP = "com.android.camera.action.CROP";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate " + this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy " + this);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
			case KeyEvent.KEYCODE_BACK:
				finish();
				break;
			default:
				break;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void finish() {
		super.finish();
		if (isAnimation) {
			finishAnim();
		}
	}

	@Override
	public void startActivityForResult(Intent intent, int requestCode) {
		super.startActivityForResult(intent, requestCode);
		startAnim();
	}

	@Override
	public void startActivity(Intent intent) {
		super.startActivity(intent);
		startAnim();
	}

	public void startAnim() {
		overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
	}

	public void finishAnim() {
		overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
	}

	/**
	 * 获取原始图片保存的Uri
	 * 
	 * @return
	 */
	protected Uri getInUri(String time) {
		String fileName = "temp_" + time + ".png";
		Uri uri = Uri.fromFile(new File(FileUtils.getFilePath() + Constant.LOCATION_HIGH_PHOTO + Constant.DIR_TEMP_FILE + fileName));
		Log.i("MySetActivity", uri.getPath());
		return uri;
	}

	/**
	 * 获取剪切图片保存的Uri
	 * 
	 * @return
	 */
	protected Uri getOutUri(String time) {
		String fileName = "IMG_" + time + ".png";
		Uri uri = Uri.fromFile(new File(FileUtils.getFilePath() + Constant.LOCATION_HIGH_PHOTO + Constant.DIR_TEMP_FILE + fileName));
		Log.i("MySetActivity", uri.getPath());
		return uri;
	}

	/**
	 * 创建图片剪切Intent
	 * 
	 * @param action
	 * @param uri
	 * @param outUri
	 * @return
	 */
	protected Intent createCropIntent(String action, Uri uri, Uri outUri) {
		Intent intent = new Intent(action);
		if (uri != null) {
			intent.setDataAndType(uri, "image/*");
		} else {
			intent.setType("image/*");
		}
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		intent.putExtra("outputX", 100);
		intent.putExtra("outputY", 100);
		intent.putExtra("scale", true);
		if (isXiaoMi()) {
			if (outUri != null) {
				intent.putExtra(MediaStore.EXTRA_OUTPUT, outUri);
			}
			intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
			intent.putExtra("return-data", false);
		} else {
			intent.putExtra("return-data", true);
		}
		intent.putExtra("noFaceDetection", true);
		return intent;
	}

	/**
	 * 判断是否小米手机
	 * 
	 * @return
	 */
	protected boolean isXiaoMi() {
		// if ("Xiaomi".equals(android.os.Build.MANUFACTURER) ||
		// "LENOVO".equals(android.os.Build.MANUFACTURER)) {
		// return true;
		// }
		// return false;
		return true;
	}

	/**
	 * dip转成px
	 * 
	 * @param dipValue
	 * @return
	 */
	public int dip2px(float dipValue) {
		float scale = getResources().getDisplayMetrics().density;
		return (int) Math.ceil(dipValue * scale);
	}

	/**
	 * px转成dip
	 * 
	 * @param pxValue
	 * @return
	 */
	public int px2dip(float pxValue) {
		float scale = getResources().getDisplayMetrics().density;
		return (int) Math.ceil(pxValue / scale);
	}

	/**
	 * 提示框
	 * 
	 * @param tooltip
	 */
	public void showTooltip(String tooltip) {
		showTooltip(tooltip, false);
	}

	/**
	 * 提示框
	 * 
	 * @param tooltip
	 * @param isClose
	 */
	public void showTooltip(String tooltip, final boolean isClose) {
		Builder b = new AlertDialog.Builder(this).setTitle("系统消息").setMessage(tooltip);
		b.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int whichButton) {
				if (isClose) {
					BaseActivity.this.finish();
				} else {
					dialog.dismiss();
				}
			}
		}).create();
		b.show();
	}

}
